package com.loto.insurance.mis.web.controller.ajax;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.loto.insurance.mis.web.authority.INSAuthority;
import com.loto.insurance.mis.web.entity.ajax.BaseJson;
import com.loto.insurance.mis.web.entity.ajax.GridJson;
import com.loto.insurance.mis.web.entity.ajax.ProductInfo;
import com.loto.insurance.mis.web.enumeration.MenuEnum;
import com.loto.insurance.mis.web.enumeration.ResultTypeEnum;
import com.loto.insurance.mis.web.service.InsCompanyService;
import com.loto.insurance.mis.web.service.ProductInfoService;
import com.loto.insurance.mis.web.service.UserInfoService;
import com.loto.insurance.mis.web.entity.InsCompany;
import com.loto.insurance.mis.web.entity.Pager;
import com.loto.utils.DateUtils;

@Controller
@SessionAttributes("ins_user")
public class ProductJsonController {
  @Autowired
  UserInfoService userInfoService;

  @Autowired
  ProductInfoService productInfoService;

  @Autowired
  InsCompanyService insCompanyService;

  @INSAuthority(authorityMenu = MenuEnum.PRODUCT_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/product/list.json", method = RequestMethod.POST)
  public ModelAndView productList(
	  @RequestParam(value = "productName", required = false) String productName,
	  @RequestParam(value = "productBegin", required = false) String productBegin,
	  @RequestParam(value = "productEnd", required = false) String productEnd,
	  @RequestParam(value = "productType", required = false, defaultValue = "-1") int productType,
	  @RequestParam(value = "page", required = false, defaultValue = "1") int page,
	  @RequestParam(value = "rows", required = false, defaultValue = "20") int rows) {
	Map<String, Object> params = new HashMap<String, Object>();
	if (productName != null && !productName.isEmpty())
	  params.put("productName", productName);
	if (productBegin != null && productEnd != null
	    && DateUtils.isDate(productBegin, "yyyy-MM-dd")
	    && DateUtils.isDate(productEnd, "yyyy-MM-dd")) {
	  params.put("productBegin", productBegin);
	  params.put("productEnd", productEnd);
	}
	if (productType > -1)
	  params.put("productType", productType);

	Pager pager = new Pager();
	pager.setPageSize(rows);

	params.put("pageSize", pager.getPageSize());
	params.put("startIndex", pager.getPageSize() * (page - 1));

	if (page > 1)
	  pager.setIndex(page);
	ArrayList<com.loto.insurance.mis.web.entity.ProductInfo> productList = productInfoService
	    .findAllProducts(params);

	ArrayList<ProductInfo> list = new ArrayList<ProductInfo>();
	for (com.loto.insurance.mis.web.entity.ProductInfo product : productList) {
	  ProductInfo p = new ProductInfo();
	  p.setCompanyShortName(product.getCompanyShortName());
	  p.setProductCreatedOn(DateUtils.formatShortDateTime(product
		  .getProductCreatedOn()));
	  p.setProductId(product.getProductId());
	  p.setProductName(product.getProductName());
	  p.setProductPrice(product.getProductPrice());
	  p.setProductSettlePrice(product.getProductSettlePrice());
	  p.setProductShortName(product.getProductShortName());
	  p.setProductValidTime(DateUtils.formatDate(product.getProductBeginTime())
		  + "  至  " + DateUtils.formatDate(product.getProductEndTime()));
	  p.setSupName(product.getSupName());
	  p.setTypeName(product.getTypeName());
	  list.add(p);
	}

	pager.setRowCount(productInfoService.findAllProductsCount(params));

	ModelAndView mav = new ModelAndView();

	GridJson<ProductInfo> gridJson = new GridJson<ProductInfo>();
	gridJson.setTotal(pager.getRowCount());
	gridJson.setRows(list);

	mav.addObject("result", gridJson);
	return mav;
  }

  @INSAuthority(authorityMenu = MenuEnum.PRODUCT_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/product/add.json", method = RequestMethod.POST)
  public ModelAndView productAdd(@RequestParam(value = "type") int type,
	  @RequestParam(value = "company") int company,
	  @RequestParam(value = "sup") long sup,
	  @RequestParam(value = "name") String name,
	  @RequestParam(value = "pic") String pic,
	  @RequestParam(value = "code") String code,
	  @RequestParam(value = "shortName") String shortName,
	  @RequestParam(value = "begin") String begin,
	  @RequestParam(value = "end") String end,
	  @RequestParam(value = "price") BigDecimal price,
	  @RequestParam(value = "settlePrice") BigDecimal settlePrice,
	  @RequestParam(value = "quota") int quota,
	  @RequestParam(value = "min") int min,
	  @RequestParam(value = "max") int max,
	  @RequestParam(value = "time") int time,
	  @RequestParam(value = "timeUnit") String timeUnit,
	  @RequestParam(value = "timeDesc") String timeDesc,
	  @RequestParam(value = "shortIntro") String shortIntro,
	  @RequestParam(value = "service") String service,
	  @RequestParam(value = "intro") String intro,
	  @RequestParam(value = "others") String others,
	  @RequestParam(value = "status") int status) {
	BaseJson baseJson = new BaseJson();
	baseJson.setOk(false);

	InsCompany companyInfo = insCompanyService.findActiveById(company);
	if (companyInfo != null) {
	  com.loto.insurance.mis.web.entity.ProductInfo productInfo = new com.loto.insurance.mis.web.entity.ProductInfo();
	  productInfo.setProductName(name);
	  productInfo.setProductPrice(price);
	  productInfo.setProductSettlePrice(settlePrice);
	  productInfo.setProductShortName(shortName);
	  productInfo.setProductAgeMax(max);
	  productInfo.setProductAgeMin(min);
	  productInfo.setProductBeginTime(DateUtils.parseDate(begin));
	  productInfo.setProductCode(code);
	  productInfo.setProductEndTime(DateUtils.parseDate(end));
	  productInfo.setProductInsFid(company);
	  productInfo.setProductIntro(intro);
	  productInfo.setProductIsActive(status);
	  productInfo.setProductOthers(others);
	  productInfo.setProductPic(pic);
	  productInfo.setProductQuota(quota);
	  productInfo.setProductServeDesc(service);
	  productInfo.setProductShortIntro(shortIntro);
	  productInfo.setProductSupFid(sup);
	  productInfo.setProductTime(time);
	  productInfo.setProductTimeDesc(timeDesc);
	  productInfo.setProductTimeUnit(timeUnit);
	  productInfo.setProductTypeFid(type);
	  productInfo.setProductEx(companyInfo.getCompanyEx());

	  productInfoService.addProductInfo(productInfo);

	  baseJson.setOk(true);
	}
	ModelAndView mav = new ModelAndView();
	mav.addObject("result", baseJson);
	return mav;
  }

  @INSAuthority(authorityMenu = MenuEnum.PRODUCT_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/product/modify.json", method = RequestMethod.POST)
  public ModelAndView productModify(@RequestParam(value = "id") long id,
	  @RequestParam(value = "type") int type,
	  @RequestParam(value = "company") int company,
	  @RequestParam(value = "sup") long sup,
	  @RequestParam(value = "name") String name,
	  @RequestParam(value = "pic") String pic,
	  @RequestParam(value = "code") String code,
	  @RequestParam(value = "shortName") String shortName,
	  @RequestParam(value = "begin") String begin,
	  @RequestParam(value = "end") String end,
	  @RequestParam(value = "price") BigDecimal price,
	  @RequestParam(value = "settlePrice") BigDecimal settlePrice,
	  @RequestParam(value = "quota") int quota,
	  @RequestParam(value = "min") int min,
	  @RequestParam(value = "max") int max,
	  @RequestParam(value = "time") int time,
	  @RequestParam(value = "timeUnit") String timeUnit,
	  @RequestParam(value = "timeDesc") String timeDesc,
	  @RequestParam(value = "shortIntro") String shortIntro,
	  @RequestParam(value = "service") String service,
	  @RequestParam(value = "intro") String intro,
	  @RequestParam(value = "others") String others,
	  @RequestParam(value = "status") int status) {
	BaseJson baseJson = new BaseJson();
	baseJson.setOk(false);

	InsCompany companyInfo = insCompanyService.findActiveById(company);
	if (companyInfo != null) {
	  com.loto.insurance.mis.web.entity.ProductInfo productInfo = new com.loto.insurance.mis.web.entity.ProductInfo();
	  productInfo.setProductId(id);
	  productInfo.setProductName(name);
	  productInfo.setProductPrice(price);
	  productInfo.setProductSettlePrice(settlePrice);
	  productInfo.setProductShortName(shortName);
	  productInfo.setProductAgeMax(max);
	  productInfo.setProductAgeMin(min);
	  productInfo.setProductBeginTime(DateUtils.parseDate(begin));
	  productInfo.setProductCode(code);
	  productInfo.setProductEndTime(DateUtils.parseDate(end));
	  productInfo.setProductInsFid(company);
	  productInfo.setProductIntro(intro);
	  productInfo.setProductIsActive(status);
	  productInfo.setProductOthers(others);
	  productInfo.setProductPic(pic);
	  productInfo.setProductQuota(quota);
	  productInfo.setProductServeDesc(service);
	  productInfo.setProductShortIntro(shortIntro);
	  productInfo.setProductSupFid(sup);
	  productInfo.setProductTime(time);
	  productInfo.setProductTimeDesc(timeDesc);
	  productInfo.setProductTimeUnit(timeUnit);
	  productInfo.setProductTypeFid(type);
	  productInfo.setProductEx(companyInfo.getCompanyEx());

	  productInfoService.updateProduct(productInfo);

	  baseJson.setOk(true);
	}
	ModelAndView mav = new ModelAndView();
	mav.addObject("result", baseJson);
	return mav;
  }
}
